const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //通告信息
    recruit_info: [],
    role: app.register_info.register_role,

    touch_endX: '',

    //开关
    flag_first: false,
    is_resume: null,
    flag_card: true,
    role: '',
    flag_contact: true,
  },
  getTab() {
    wx.request({
      url: app.globalData.url + 'api/timing/applet/show',
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let is_show = e.data.data.is_show,
          tel = wx.getStorageSync('reg_telephone');
        if (is_show == app.globalData.tab_is_show) {
          this.setData({
            tabbar: {
              color: "#fff",
              selectedColor: "#d0a76c",
              backgroundColor: "#1b1b1b",
              borderStyle: "black",
              list: [{
                  pagePath: "../../../pages/industry/industry",
                  text: "行业",
                  iconPath: "../../../images/icon/icon_ind.png",
                  selectedIconPath: "../../../images/icon/icon_indS.png",
                  selected: false
                },
                {
                  pagePath: "../../../pages/mine/mine",
                  text: "我的",
                  iconPath: "../../../images/icon/icon_mine.png",
                  selectedIconPath: "../../../images/icon/icon_mineS.png",
                  selected: true
                }
              ],
              position: "bottom"
            },
          })
          this.editTabBar();
        } else {
          this.setData({
            tabbar: {
              color: "#fff",
              selectedColor: "#d0a76c",
              backgroundColor: "#1b1b1b",
              borderStyle: "black",
              list: [{
                  pagePath: "../../../pages/recruit/index/index",
                  text: "招聘",
                  iconPath: "../../../images/icon/icon_rec.png",
                  selectedIconPath: "../../../images/icon/icon_recS.png",
                  selected: false
                },
                {
                  pagePath: "../../../pages/recruit/announce/announce",
                  text: "通告",
                  iconPath: "../../../images/icon/icon_announce.png",
                  selectedIconPath: "../../../images/icon/icon_announceS.png",
                  selected: true
                },
                {
                  pagePath: "../../../pages/industry/industry",
                  text: "行业",
                  iconPath: "../../../images/icon/icon_ind.png",
                  selectedIconPath: "../../../images/icon/icon_indS.png",
                  selected: false
                },
                {
                  pagePath: "../../../pages/mine/mine",
                  text: "我的",
                  iconPath: "../../../images/icon/icon_mine.png",
                  selectedIconPath: "../../../images/icon/icon_mineS.png",
                  selected: false
                }
              ],
              position: "bottom"
            },
          })
          this.editTabBar();
        }
      },
    })
  },
  //切换tabbar的方法  
  editTabBar: function() {
    var tabbar = this.data.tabbar,
      currentPages = getCurrentPages(),
      _this = currentPages[currentPages.length - 1],
      pagePath = _this.__route__;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (var i in tabbar.list) {
      // tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
    // console.log(tabbar)
  },
  //七日收益
  earns() {
    wx.request({
      url: app.globalData.url + 'api/task/seven/day/earnings',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        let earnsInfo = e.data.data.earnings;
        var xInfo = [],
          yInfo = [];
        earnsInfo.forEach(function(value, index) {
          var str = value.show_date;
          var str_new = str.replace(/"-"/, ".");
          var str_re = str_new.substring(5);
          value.show_date = str_re;
          value.operate_num = parseInt(value.operate_num);
          xInfo.push(value.show_date);
          yInfo.push(value.operate_num)
        })
        // outSide.xInfo = xInfo;
        // outSide.yInfo = yInfo;
        wx.setStorageSync('xInfo', xInfo)
        wx.setStorageSync('yInfo', yInfo)

        // console.log(outSide.xInfo)
        // console.log(outSide.yInfo)
      }
    })
  },
  //去福利页
  go_welfare() {
    this.earns();
    wx.navigateTo({
      url: '../../mine/invite/index',
      // url: '../../boMoney/index',
    })
  },
  //click_apply
  click_apply(e) {
    // console.log(e)
    if (wx.getStorageSync('token') == '') {
      wx.showModal({
        content: '登录后才能体验哦~',
        showCancel: true,
        cancelColor: '#333',
        confirmText: '登录',
        confirmColor: '#d0a76c',
        success: function(e) {
          if (e.confirm) {
            wx.redirectTo({
              url: '../../author/author',
            })
          }
        },
      })

    } else {
      let that = this;
      var anchor_from = e.currentTarget.dataset.anchor_from,
        is_apply = e.currentTarget.dataset.is_apply,
        is_status = e.currentTarget.dataset.is_status,
        conscribe_id = e.currentTarget.dataset.conscribe_id;
      if (is_status == "2") {
        // console.log(is_status)
        if (anchor_from == "1") {
          //只能主播
          if (that.data.role != "2") {
            wx.showToast({
              title: '该通告仅支持主播报名，会长角色不支持报名操作。',
              icon: 'none',
              duration: 1000,
            })
            console.log(that.data.role)
          } else {
            if (is_apply == "2") {
              wx.navigateTo({
                url: '../apply/apply?conscribe_id=' + conscribe_id,
              })
            } else {
              wx.showToast({
                title: '您已报名噢~',
                icon: 'none',
                duration: 800,
              })
            }
          }
        } else if (anchor_from == "2") {
          //会长、管理员、主播
          if (is_apply == "2") {
            wx.navigateTo({
              url: '../apply/apply?conscribe_id=' + conscribe_id,
            })
          } else {
            wx.showToast({
              title: '您已报名噢~',
              icon: 'none',
              duration: 800,
            })
          }
        } else {}
      }
    }

  },
  //去 填写简历
  go_uploadResume: function() {
    this.setData({
      flag_card: true,
    })
    wx.navigateTo({
      url: '../../resume/uploadResume/uploadResume',
    })
  },
  //模态框 拨打
  click_phone: function() {
    //管理员、会长
    wx.makePhoneCall({
      phoneNumber: '0571-87081736',
    })
    this.setData({
      flag_contact: true,
    })
  },
  //模态框 复制
  click_modal_copy: function() {
    wx.setClipboardData({
      data: 'zhibozhijia888',
      success: function(e) {}
    })
    this.setData({
      flag_contact: true,
    })
  },
  //隐藏
  modal_hide: function() {
    this.setData({
      flag_card: true,
      flag_contact: true,
    })
  },
  //抢单 判断
  click_robbing: function() {
    console.log(123)
    let that = this;
    if (this.data.role != 2) {
      that.setData({
        flag_contact: false,
      })
    } else if (this.data.role == 2) {
      that.setData({
        flag_card: false,
      })
    } else {}


  },
  card_start: function(e) {
    let that = this;
    // console.log('开始+' + e.touches["0"].pageX)
    this.setData({
      touch_startX: e.touches["0"].pageX,
      recruit_id: e.currentTarget.dataset.recruit_id,
    })
  },
  card_end: function(e) {
    let that = this;
    // console.log('结束+' + e.changedTouches["0"].pageX)
    this.setData({
      touch_endX: e.changedTouches["0"].pageX,
    })
    let distance = this.data.touch_endX - this.data.touch_startX
    if (distance > 100) {
      //右划
      this.setData({
        flag_first: false,
      })
      let recruit_info = this.data.recruit_info;
      recruit_info.shift();
      this.setData({
        recruit_info: recruit_info
      })
      if (this.data.recruit_info.length == 0) {
        this.get_info()
      }

    } else if (distance < -100) {
      //左滑
      this.setData({
        flag_first: false,
      })
      let recruit_info = this.data.recruit_info;
      recruit_info.shift();
      this.setData({
        recruit_info: recruit_info
      })
      if (this.data.recruit_info.length == 0) {
        this.get_info()
      }
    }
    // console.log(distance)
    // console.log(this.data.flag_first)
  },
  //是否有简历
  get_resume: function() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/list',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e.data.data.is_resume)
        that.setData({
          is_resume: e.data.data.is_resume,
        })
      },

    })
  },
  //请求通告
  get_info: function() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/timing/conscribe/index',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e.data.data.conscribe)
        that.setData({
          recruit_info: e.data.data.conscribe
        })

        that.setData({
          flag_first: true,
        })

      },

    })
  },
  //埋点
  secret() {
    wx.request({
      url: app.globalData.url + 'api/timing/common/oncilck/bottom',
      data: {
        token: app.globalData.token,
        from: 'WxMiNi',
        type: '1'
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.earns();
    this.getTab();
    this.setData({
      role: app.register_info.register_role,
    })
    console.log('role+' + app.register_info.register_role)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.get_info();
    // this.get_resume();
    this.setData({
      flag_first: true,
    })
    if (app.globalData.token) {
      this.get_resume();
      this.earns();
      this.secret();
    }
    console.log(this.data.flag_first);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})